//
//  RemainingViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/9.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class RemainingViewController: RefreshedTableViewController {
    
    let approvalrefresh = "approvalrefresh"
    
    let approvalloadmore = "approvalloadmore"
    
    var approvalsource: [JSON] = [] //审批等待办
    
    let projectapprovalrefresh = "projectapprovalrefresh"
    
    let projectapprovalloadmore = "projectapprovalloadmore"
    
    var projectsource: [JSON] = [] //项目待办
    
    let finacialApprovalRefresh = "finacialApprovalrefresh"
    
    let finacialApprovalLoadMore = "finacialApprovalloadmore"
    
    var finacialsource: [JSON] = [] //采购待办
    
    let bidApprovalRefresh = "bidrefresh"
    
    let bidApprovalLoadMore = "bidloadmore"
    
    var bidsource: [JSON] = [] //投标待办
    
    let employeePurchasingRefresh = "employeerefresh"
    let employeePurchasingLoadNext = "employeeloadnext"
    var employeePurchasingSource: [JSON] = []
    
    let AllBXListRefresh = "bxrefresh"
    let AllBXListMore = "bxloadnext"
    var AllBXSource: [JSON] = []
    
    var _source: Array<JSON> = []
    
    var approvalTotalCount = 0
    
    var approvalPage = 1
    
    var projectTotalCount = 0
    
    var projectPage = 1
    
    var finacialApprovalCount = 0
    
    var finacialApprovalPage = 1
    
    var bidApprovalCount = 0
    
    var bidPage = 1
    
    var employeeCount = 0
    
    var employeePage = 1
    
    var AllBXCount = 0
    var AllBXPage = 1
    
    override var totalCount: Int{
        get{
            return approvalTotalCount + projectTotalCount + finacialApprovalCount + bidApprovalCount + employeeCount + AllBXCount
        }
        set{
            
        }
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.tableView.register(UINib(nibName: "RemainingTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "RemainingTableViewCell")
        
        NotificationCenter.default.addObserver(self, selector: #selector(beginRefresh), name: NSNotification.Name("RefreshDetail"), object: nil)

        beginRefresh()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override var names: Array<String>{
        return [approvalrefresh,approvalloadmore,projectapprovalrefresh,projectapprovalloadmore,finacialApprovalRefresh,finacialApprovalLoadMore]
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        if name.hasSuffix("refresh") {
            return ["curPage":1,"pageSize":"20","approvalStatus": "0","orgCode": self.orgCode]
        }
        else if name == approvalloadmore {
            
            return ["approvalStatus":"0","curPage":String(approvalPage + 1),"pageSize":"10"] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]

        } else if name == projectapprovalloadmore {
            
            return ["approvalStatus":"0","curPage":String(projectPage + 1),"pageSize":"10"] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]
        } else if name == finacialApprovalLoadMore {
            return ["approvalStatus":"0","curPage":String(finacialApprovalPage + 1),"pageSize":"10"] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]
        } else if name == bidApprovalRefresh {
            return ["approvalStatus":"0","curPage":1,"pageSize":10] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]
        } else if name == bidApprovalLoadMore {
            return ["approvalStatus":"0","curPage": bidPage,"pageSize":10] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]
        } else if name == employeePurchasingLoadNext {
            return ["approvalStatus":"0","curPage": employeePage + 1,"pageSize":10] + ["beginTime": (searchBar.text ?? ""),"orgCode": orgCode]
        } else if name == AllBXListRefresh {
            return ["curPage":1,"pageSize":"20","approvalStatus": "0","orgCode": self.orgCode]
        } else if name == AllBXListMore {
            return ["curPage":AllBXPage + 1,"pageSize":"20","approvalStatus": "0","orgCode": self.orgCode]
        }
        
        return [:];
    }
    
    override func success(for name: String) -> NetSuccess {
        
        func refreshApprovalSuccess(json: JSON)->Void{
            
            self.tableView.endAnimation()
            
            if let success = json.contents?["success"]?.bool {
                
                if success == false {
                    
                    self.alert(json.contents?["errorMsg"]?.rawString())
                    
                    return
                }
                
            }
            
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.approvalsource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.approvalTotalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.approvalsource = []
            for item in list {
                self.approvalsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self._source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func refreshProjectApprovalSuccess(json: JSON)->Void{
            
            self.tableView.endAnimation()
            
            if let success = json.contents?["success"]?.bool {
                if success == false {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                    return
                }
            }
            
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.projectsource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.projectTotalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            print(list)
            self.projectsource = []
            
            for item in list {
                self.projectsource.append(item)
            }
            
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            
            if self._source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func loadApprovalMoreSuccess(json: JSON)->Void{
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.approvalTotalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            if list.count > 0 {
                self.approvalPage += 1
            }
            print(list)
            for item in list {
                self.approvalsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            
            if self.totalCount <= self.source.count {
                //configfooter
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }
            
            self.tableView.reloadData()
        }
        
        func loadProjectApprovalMoreSuccess(json: JSON)->Void{
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.projectTotalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            if list.count > 0 {
                self.projectPage += 1
            }
            
            for item in list {
                self.projectsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self.totalCount <= self._source.count {
                //configfooter
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }
            
            self.tableView.reloadData()
        }
        
        func finacialRefresh(json: JSON)->Void{
            self.tableView.endAnimation()

            if let success = json.contents?["success"]?.bool {

                if success == false {

                    self.alert(json.contents?["errorMsg"]?.rawString())

                    return
                }

            }

            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }

            self.finacialApprovalCount = total
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.finacialsource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }

            self.finacialsource = []

            for item in list {
                self.finacialsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }

            if self.source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func finacialLoadMore(json: JSON)->Void{
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.finacialApprovalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            if list.count > 0 {
                self.finacialApprovalPage += 1
            }
            
            for item in list {
                self.finacialsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self.totalCount <= self.source.count {
                //configfooter
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }
            
            self.tableView.reloadData()
        }
        
        func bidrefresh(json: JSON)->Void{
            self.tableView.endAnimation()
            
            if let success = json.contents?["success"]?.bool {
                
                if success == false {
                    
                    self.alert(json.contents?["errorMsg"]?.rawString())
                    
                    return
                }
            }
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            self.bidApprovalCount = total
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.bidsource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.bidsource = []
            
            for item in list {
                self.bidsource.append(item)
            }
            
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self.source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func bidloadnext(json: JSON)->Void{
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.bidApprovalCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            if list.count > 0 {
                self.bidPage += 1
            }
            
            for item in list {
                self.bidsource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            
            if self.totalCount <= self.source.count {
                //configfooter
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }
            
            self.tableView.reloadData()
        }
        
        func employeerefresh(json: JSON){
            self.tableView.endAnimation()
            
            if let success = json.contents?["success"]?.bool {
                
                if success == false {
                    
                    self.alert(json.contents?["errorMsg"]?.rawString())
                    
                    return
                }
            }
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.employeePurchasingSource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            self.employeeCount = total
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.employeePurchasingSource = []
            
            for item in list {
                self.employeePurchasingSource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self.source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func employeeloadnext(json: JSON){
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            self.employeeCount = total
            
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            
            if list.count > 0 {
                self.employeePage += 1
            }
            
            for item in list {
                self.employeePurchasingSource.append(item)
            }
            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }
            if self.totalCount <= self.source.count {
                //configfooter
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }
            
            self.tableView.reloadData()
        }
        
        func allbxrefresh(json: JSON){
            self.tableView.endAnimation()

            if let success = json.contents?["success"]?.bool {

                if success == false {

                    self.alert(json.contents?["errorMsg"]?.rawString())

                    return
                }
            }
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.AllBXSource = []
                self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
                self._source = self._source.sorted { (j1, j2) -> Bool in
                    let dt1 = j1["createTime"].string ?? ""
                    let dt2 = j2["createTime"].string ?? ""
                    let formatter = DateFormatter.init()
                    formatter.dateFormat = "yyyy-mm-dd"
                    formatter.locale = Locale.init(identifier: "zh_CN")
                    let date1 = formatter.date(from: dt1) ?? Date()
                    let date2 = formatter.date(from: dt2) ?? Date()
                    return date1.compare(date2) == .orderedDescending
                }
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }
            self.AllBXCount = total
            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }

            self.AllBXSource = []

            for item in list {
                self.AllBXSource.append(item)
            }

            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }

            if self.source.count == 0 {
                self.tableView.tableFooterView = UIView()
            }
            self.tableView.reloadData()
        }
        
        func allbxnext(json: JSON){
            self.tableView.endAnimation()
            guard let total = json.contents?["data"]?.contents?["total"]?.rawValue as? Int else {
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }

            self.AllBXCount = total

            guard let list = json.contents?["data"]?.contents?["list"]?.array else {
                //刷新失败此时
                self.tableView.reloadData()
                self.tableView.tableFooterView = UIView()
                return
            }

            if list.count > 0 {
                self.AllBXPage += 1
            }

            for item in list {
                self.AllBXSource.append(item)
            }

            self._source = approvalsource + projectsource + finacialsource + bidsource + employeePurchasingSource
            self._source = self._source.sorted { (j1, j2) -> Bool in
                let dt1 = j1["createTime"].string ?? ""
                let dt2 = j2["createTime"].string ?? ""
                let formatter = DateFormatter.init()
                formatter.dateFormat = "yyyy-mm-dd"
                formatter.locale = Locale.init(identifier: "zh_CN")
                let date1 = formatter.date(from: dt1) ?? Date()
                let date2 = formatter.date(from: dt2) ?? Date()
                return date1.compare(date2) == .orderedDescending
            }

            if self.totalCount <= self.source.count {
                let footer = UILabel()
                footer.font = UIFont.systemFont(ofSize: 14)
                footer.text = "没有更多数据"
                footer.textAlignment = .center
                footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 40))
                footer.backgroundColor = UIColor.hex("eeeeee")
                footer.textColor = UIColor.hex("999999")
                self.tableView.tableFooterView = footer
            } else {
                self.tableView.tableFooterView = UIView()
            }

            self.tableView.reloadData()
        }
        
        switch name {
        case approvalrefresh:
            return refreshApprovalSuccess
        case approvalloadmore:
            return loadApprovalMoreSuccess
        case projectapprovalrefresh:
            return refreshProjectApprovalSuccess
        case projectapprovalloadmore:
            return loadProjectApprovalMoreSuccess
        case finacialApprovalRefresh:
            return finacialRefresh
        case finacialApprovalLoadMore:
            return finacialLoadMore
        case bidApprovalRefresh:
            return bidrefresh
        case bidApprovalLoadMore:
            return bidloadnext
        case employeePurchasingRefresh:
            return employeerefresh
        case employeePurchasingLoadNext:
            return employeeloadnext
        case AllBXListRefresh:
            return allbxrefresh
        case AllBXListMore:
            return allbxnext
        default:
            func dft(json: JSON) -> Void {
                
            }
            return dft
        }
    }
    
    override func path(for name: String) -> String {
        if name == approvalrefresh || name == approvalloadmore{
            return Ppproval
        } else if name == projectapprovalrefresh || name == projectapprovalloadmore {
            return ProjectModifyApproval
        } else if name == finacialApprovalRefresh || name == finacialApprovalLoadMore{
            return ApprovalList
        } else if name == bidApprovalRefresh || name == bidApprovalLoadMore {
            return BidApprovalList
        } else if name == employeePurchasingRefresh || name == employeePurchasingLoadNext {
            return EmployeePurchasingApprovalList
        } else if name == AllBXListRefresh || name == AllBXListMore {
            return ALLBXList
        }
        return ""
    }
    
    override func beginRefresh() {
        self.load(with: approvalrefresh)
        self.load(with: projectapprovalrefresh)
        self.load(with: finacialApprovalRefresh)
        self.load(with: bidApprovalRefresh)
//        self.load(with: employeePurchasingRefresh)
        self.load(with: AllBXListRefresh)
    }
    
    override func load(with name: String) {
        let canReach = NetManager.isActive
        if canReach == false {
            if name.contains("refresh") {
                self.nothavenet()
                self.finacialsource = []
                self.projectsource = []
                self.approvalsource = [] //审批等待办
                self.tableView.endAnimation()
                self.tableView.reloadData()
            }
            self.failBlock(for: name)(NSError.init(domain: "NetError", code: -1, userInfo: ["NetReachability":false]))
        }
        let result = strategy(for: name)
        if result.0 == false {
            if result.2 != nil {
                result.2!();
            }else{
                self.alert(result.1)
            }
            return
        }
        self.beginLoading()
        switch self.method(for: name) {
        case .get:
            print(self.param(for: name))
            NetManager.get(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
            break
        case .upload:
            let files = self.files(for: name)
            for file in files {
                NetManager.upload(self.path(for: name), self.param(for: name), [file], name: filename(for: name), suc: { (source) in
                    self.endLoading()
                    self.success(for: name)(source)
                }, fail: { (error) in
                    self.endLoading()
                    self.failBlock(for: name)(error)
                })
            }
        default:
            NetManager.post(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
        }
    }
    
    override func loadNext() {
        self.load(with: approvalloadmore)
        self.load(with: projectapprovalloadmore)
        self.load(with: finacialApprovalLoadMore)
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "RemainingTableViewCell")
        
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, willDisplay cel: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let json = _source[indexPath.row]
        
        guard let cell = cel as? RemainingTableViewCell else { return }
        let type = json.contents?["businessType"]?.int ?? 0
        var taskName = json.contents?["projectname"]?.rawString() ?? ""
        if type == 10 {
            taskName = json.contents?["taskName"]?.rawString() ?? ""
        }
        
        cell.titleLbl.text = (json.contents?["businessTypeName"]?.rawString() ?? "") + "-" + taskName
        
        cell.timeLbl.text = json.contents?["applyTime"]?.rawString() ?? ""
        
        cell.triggerLbl.text = json.contents?["applyName"]?.rawString() ?? ""
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75.0
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return _source.count
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let json = _source[indexPath.row]
        
        let businessType = json.contents?["businessType"]?.rawValue as? Int ?? -1
        
        if businessType == 20 {
            //支付
            let container = PayApprovalDetailViewController()
            
            container.applyid = json.contents?["pid"]?.rawString() ?? ""
            
            container.applyType = String(businessType)
            
            container.title = "付款详情"
            
            self.push(destination: container)
            
        } else if businessType == 10 {
            //采购审批
            let container = ApprovalDetailViewController()
            
            container.applyid = json.contents?["pid"]?.rawString() ?? ""
            
            container.applyType = String(businessType)
            
            container.title = (json.contents?["businessTypeName"]?.rawString() ?? "") + "详情"
            
            self.push(destination: container)
            
        } else if businessType == 30 {
            //立项
            let container = CreateProjectViewController()
            
            container.applyid = json.contents?["pid"]?.rawString() ?? ""
            
            container.applyType = String(businessType)
            
            container.title = (json.contents?["businessTypeName"]?.rawString() ?? "") + "详情"
            
            container.canApproval = true
            
            self.push(destination: container)
            
        } else if businessType == 40 {
            //变更
            let container = ProjectModifyViewController()
            
            container.applyid = json.contents?["pid"]?.rawString() ?? ""
            
            container.applyType = String(businessType)
            
            container.title = (json.contents?["businessTypeName"]?.rawString() ?? "") + "详情"
            
            self.push(destination: container)
            
        } else if businessType == 50 {
            //报销
            let container = ReimbursementDetailViewController()
            container.id = json.contents?["pid"]?.rawString() ?? ""
            container.title = "报销详情"
            container.ifneedapproal = true
            self.push(destination: container)
        } else if businessType == 60 {
            //结项审批单
            let container = EndProjectViewController()
            
            container.applyId = json.contents?["pid"]?.rawString() ?? ""
            
            container.title = (json.contents?["businessTypeName"]?.rawString() ?? "") + "详情"
            
            self.push(destination: container)
        }
        else if businessType == 140 {
            //投标审批
            let container = BidDetailViewController()
            container.id = json.contents?["pid"]?.string ?? ""
            container.title = "投标审批详情"
            self.push(destination: container)
        } else if businessType == 70 {
            //人员需求审批
            let container = EmployeeDetailViewController()
            container.id = json["pid"].string ?? ""
            print(container.id)
            container.title = "人员需求详情"
            self.push(destination: container)
        } else if businessType == 150 {
            //结算
            let container = EndBudgetViewController()
            
            container.id = json.contents?["pid"]?.rawString() ?? ""
            
            container.title = (json.contents?["businessTypeName"]?.rawString() ?? "") + "详情"
            
            self.push(destination: container)
        } else if businessType == 190 {
            let bx = BaoXiaoDetailViewController()
            bx.id = json.contents?["pid"]?.rawString() ?? ""
            bx.title = "报销详情"
            bx.canapproval = true
            self.push(destination: bx)
        } else {
            self.alert("暂未开放")
        }
    }
}
